﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using TED.ViewModels;
using Microsoft.Phone.Shell;
using TED.Model;

namespace TED.View
{
    public partial class SearchPage : PhoneApplicationPage
    {
        private SearchViewModel svm;
        private VideoViewModel vvm;

        public SearchPage()
        {
            InitializeComponent();
            svm = new SearchViewModel();
            vvm = new VideoViewModel();
            this.SearchPivot.DataContext = svm;
            this.Loaded += new RoutedEventHandler(SearchPage_Loaded);
        }

        void SearchPage_Loaded(object sender, RoutedEventArgs e)
        {
            this.searchTextBox.Focus();
            TalksSearchedOnPage.DataContext = svm.TalkSearchedItems;
            
            svm.ElementFinished += new SearchViewModel.ElementFinishedDelegate(SearchViewModel_ElementFinished);
            svm.WorkFinished += new SearchViewModel.WorkFinishedDelegate(SearchViewModel_WorkFinished);
        }

        void SearchViewModel_ElementFinished()
        {
            
        }

        void SearchViewModel_WorkFinished(bool success)
        {
            this.LoadingTextBlock.Visibility = Visibility.Collapsed;
            this.LoadingTextBlock2.Visibility = Visibility.Collapsed;
        }

        private void searchTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                svm.SearchString = searchTextBox.Text.ToString();
                svm.DoSearch();
                this.Focus();
            }
        }

        private void GestureDoubleTap_PlayVideo(object sender, GestureEventArgs e)
        {
            ListBoxItem selectedListBoxItem = this.TalksSearchedOnPage.ItemContainerGenerator.ContainerFromItem
                ((sender as StackPanel).DataContext) as ListBoxItem;

            if (selectedListBoxItem == null)
            {
                return;
            }
            string author = null;
            if (((TEDVideo)selectedListBoxItem.Content).Author != null)
                author = ((TEDVideo)selectedListBoxItem.Content).Author.ToString();

            string title;
            if (((TEDVideo)selectedListBoxItem.Content).Title != null)
                title= ((TEDVideo)selectedListBoxItem.Content).Title.ToString();
            else
                return;

            string viewUrl;
            if (((TEDVideo)selectedListBoxItem.Content).ViewUrl != null)
               viewUrl = ((TEDVideo)selectedListBoxItem.Content).ViewUrl.ToString();
            else
                return;

            Uri imageUrl = null;
            if (((TEDVideo)selectedListBoxItem.Content).ImageUrl != null)
                imageUrl = new Uri(((TEDVideo)selectedListBoxItem.Content).ImageUrl.ToString(), UriKind.Relative);


            string videoUrl = null;
            if (((TEDVideo)selectedListBoxItem.Content).VideoUrl != null)
                viewUrl = ((TEDVideo)selectedListBoxItem.Content).VideoUrl.ToString();

            //TEDVideo t = new TEDVideo(title, author, imageUrl, viewUrl, videoUrl);
            TEDVideo t = (new TEDVideo
            {
                Title = title,
                Author = author,
                ViewUrl = viewUrl,
                ImageUrl = imageUrl,
                VideoUrl = videoUrl,
            });

            vvm.AddVideoToPlaylist(t);

            (Application.Current.RootVisual as PhoneApplicationFrame).Navigate(new Uri("/Video.xaml", UriKind.Relative));

            MessageBox.Show("You double tap it");

        }

        private void ViewFavoriteVideos_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/View/Favourite.xaml", UriKind.Relative));
        }

        private void ViewSearchPage_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/View/SearchPage.xaml", UriKind.Relative));
        }

        private void PlayVideoFromPlayList_Click(object sender, EventArgs e)
        {
            this.NavigationService.Navigate(new Uri("/Video.xaml", UriKind.Relative));
        }

        private void ViewMainPage_Click(object sender, EventArgs e)
        {
            this.NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
        }
 
    }
}